package com.happyass.service.impl;

import com.happyass.dao.BookingDao;
import com.happyass.pojo.Booking;
import com.happyass.service.BookinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class BookinfoServiceImpl implements BookinfoService {
    @Autowired
    BookingDao bookingDao;

    @Override
    public HashMap<String, Object> findAll(Booking booking, int page) {
        //查询
//        Booking newbooking = new Booking();
//        newbooking.setBookingId(1);
        if(booking.getBookingName()=="") booking.setBookingName(null);
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("bookingId",match -> match.contains())
                .withMatcher("bookingCode",ExampleMatcher.GenericPropertyMatchers.exact())
                .withMatcher("bookingName",ExampleMatcher.GenericPropertyMatchers.exact())
                .withMatcher("bookdingStatus",ExampleMatcher.GenericPropertyMatchers.exact())
                .withMatcher("bookingChangeStatu",ExampleMatcher.GenericPropertyMatchers.exact())
                .withMatcher("bookingRefundStatus",ExampleMatcher.GenericPropertyMatchers.exact());
        Example<Booking> example = Example.of(booking,matcher);
        Pageable pageable = PageRequest.of(page,10);
        //分页对象
        Page<Booking> booksInfosPage =bookingDao.findAll(example,pageable);
        //当前页的集合
        List<Booking> booksList = booksInfosPage.getContent();
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("booksList",booksList);
        //获得当前页
        map.put("nowPage",booksInfosPage.getNumber()+1);
        //获得前一页
        if(page>0)
            map.put("lastPage",booksInfosPage.getNumber()-1);
        else map.put("lastPage",0);
        //获得下一页
        if(page<booksInfosPage.getTotalPages()-1)
            map.put("nextPage",booksInfosPage.getNumber()+1);
        else map.put("nextPage",booksInfosPage.getNumber());
        //获得总页码
        map.put("conPage",booksInfosPage.getTotalPages());
        //获得总数据条数
        map.put("conEle",booksInfosPage.getTotalElements());
        return  map;
    }

    @Override
    public void delDateBooking(int bookingId) {
        bookingDao.deleteById(bookingId);
    }

    @Override
    public List<Booking> findAllByDate(Date bookingTime) {
        Booking booking = new Booking();
        booking.setBookingTime(bookingTime);
//        ExampleMatcher matcher = ExampleMatcher.matchingAny()
//                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING);
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withNullHandler(ExampleMatcher.NullHandler.IGNORE)
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase()
                .withIgnoreNullValues();
        Example<Booking> example = Example.of(booking,matcher);
        List<Booking> bookings =bookingDao.findAll(example);
        return bookings;
    }

    @Override
    public void addDateBooking(Booking booking) {
        bookingDao.save(booking);
    }

    @Override
    public List<Booking> findAllByTel(String bookingTel) {
        Booking booking = new Booking();
        booking.setBookingTel(bookingTel);
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withMatcher("bookingTel",ExampleMatcher.GenericPropertyMatchers.exact());
        Example<Booking> example = Example.of(booking,matcher);
        List<Booking> bookings =bookingDao.findAll(example);
        return bookings;
    }

    @Override
    public void upDate() {
        List<Booking> bookings = bookingDao.findAll();
        for (Booking b:bookings) {
            Date bookingTime= b.getBookingTime();
            Date  nowTime = new Date();
            if(bookingTime.compareTo(nowTime)==-1 && b.getBookdingStatus()==0) {
                b.setBookdingStatus(1);
                bookingDao.save(b);
            }
        }
    }

    @Override
    public Booking findById(int bookingId) {
        Booking booking = bookingDao.findById(bookingId).orElse(null);
        return booking;
    }

}
